package com.sf.asp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sf.asp.core.exception.AspException;
import com.sf.asp.core.exception.ResourceNotFoundException;
import com.sf.asp.dao.UserMapper;
import com.sf.asp.entity.User;
import com.sf.asp.entity.UserRole;
import com.sf.asp.entity.dto.DeleteDTO;
import com.sf.asp.entity.dto.PageDTO;
import com.sf.asp.entity.dto.UserDTO;
import com.sf.asp.entity.dto.UserQueryDTO;
import com.sf.asp.entity.vo.PageInfo;
import com.sf.asp.entity.vo.UserVO;
import com.sf.asp.service.UserRoleService;
import com.sf.asp.service.UserService;
import org.apache.tomcat.jni.Local;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jins
 * @since 2021-11-24
 */
@Service
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;

    private final UserRoleService userRoleService;


    public UserServiceImpl(UserMapper userMapper,UserRoleService userRoleService){
        this.userMapper = userMapper;
        this.userRoleService = userRoleService;
    }

    @Override
    public PageInfo listUser(UserQueryDTO param) {

        Page<User> pageInfo = new Page<>(param.getPageNo(), param.getPageSize());
        IPage<User> users  = userMapper.listUser(pageInfo, param);

        List<UserVO> userShows = new ArrayList<>();
        UserVO userVO;
        for (User user : users.getRecords()){
            userVO = new UserVO();
            BeanUtils.copyProperties(user,userVO);
            userShows.add(userVO);
        }
        return new PageInfo(users.getTotal(),userShows);
    }

    /**
     * 保存用户数据
     * @param userParam 用户参数
     */
    @Override
    public void saveUser(UserDTO userParam) {
        User user = new User();
        BeanUtils.copyProperties(userParam,user);
        userMapper.saveUser(user);

        if (userParam.getRoles() != null && !userParam.getRoles().isEmpty()){
            List<UserRole> userRoles = new ArrayList<>();
            userParam.getRoles().forEach(roleId -> {
                UserRole userRole = new UserRole();
                userRole.setUserId(user.getId());
                userRole.setRoleId(roleId);
                userRole.setUserId(0);
                userRole.setCreateTime(LocalDateTime.now());
                userRoles.add(userRole);
            });
            userRoleService.saveBatch(userRoles);
        }
    }

    @Override
    public UserVO getUserById(Integer userId) {
        User user = userMapper.selectById(userId);
        if (user == null){
            throw new ResourceNotFoundException("该用户信息不存在");
        }
        UserVO userShow = new UserVO();
        BeanUtils.copyProperties(user,userShow);
        return userShow;
    }

    /**
     * 修改用户信息
     * @param userParam 用户参数
     */
    @Override
    public void editUser(UserVO userParam) {
        userMapper.editUser(userParam);
    }

    /**
     * 批量删除用户
     * @param userIds 用户id
     */
    @Override
    public void deleteUsers(DeleteDTO userIds) {
        //TODO 把删除做成一个通用对象使用
        userMapper.deleteUsers(userIds.getIds());
    }

    @Override
    public User getUserByUsername(String username) {
        LambdaQueryWrapper<User> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,username);
        return userMapper.selectOne(queryWrapper);
    }


}
